//
//  ST_BirthdayPickerView.swift
//  BTVideo
//
//  Created by DouLei on 2019/10/18.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift

class ST_BirthdayPickerView: UIView {
    
    static let height = 250+BTSafeAreaBottom
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = Color.init(hexString: "#F2F4F7")
        
        self.addSubview(top_view)
        top_view.snp.makeConstraints({ (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(44)
        })
        
        self.addSubview(confirm_picker_btn)
        confirm_picker_btn.snp.makeConstraints({ (make) in
            make.right.equalTo(self).offset(-50)
            make.width.height.equalTo(44)
            make.top.equalTo(self)
        })
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints({ (make) in
            make.centerY.equalTo(confirm_picker_btn)
            make.centerX.equalToSuperview()
        })
        
        self.addSubview(cancel_picker_btn)
        cancel_picker_btn.snp.makeConstraints({ (make) in
            make.left.equalTo(self).offset(50)
            make.width.height.equalTo(44)
            make.top.equalTo(self)
        })
        
        self.addSubview(datePicker)
        datePicker.snp.makeConstraints({ (make) in
            make.top.equalTo(self.cancel_picker_btn.snp.bottom).offset(10)
            make.left.right.equalTo(self)
            make.bottom.equalTo(self).offset(-BTSafeAreaBottom)
        })
        
        let line = UIView()
        self.addSubview(line)
        line.backgroundColor = Color.init(hexString: "#E7ECF4")
        line.snp.makeConstraints({ (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(1)
        })
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
    lazy var top_view:UIView = {
       let control = UIView()
        control.backgroundColor = Color.white
        return control
    }()
    
    lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = "请选择生日"
        control.textColor = Color.init(hexString: "#333333")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        return control
    }()
    
    lazy var confirm_picker_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("完成")
        control.setTitleColorForAllStates(Color.init(hexString: "#333333")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(confirm_picker_action), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    lazy var cancel_picker_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("取消")
        control.setTitleColorForAllStates(Color.init(hexString: "#999999")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.addTarget(self, action: #selector(cancel_picker_action), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    
    lazy var datePicker:UIDatePicker = {
        let control = UIDatePicker()
        //将日期选择器区域设置为中文，则选择器日期显示为中文
        control.locale = Locale(identifier: "zh_CN")
        control.datePickerMode = .date
        if #available(iOS 13.4, *) {
            control.preferredDatePickerStyle = .wheels
        } else {
            // Fallback on earlier versions
        }
        //        control.addTarget(self, action: #selector(dateChanged),
        //                             for: .valueChanged)
        control.maximumDate = self.minimum_birhday
        
        //        control.setValue(Color.white, forKey: "textColor")
        
        return control
    }()
    /**
     注册用户最小17岁
     */
    lazy var minimum_birhday:Date = {
        let date = Date().adding(Calendar.Component.year, value: -18)
        return date
    }()
    
    func setBirthDay(date:Date){
        self.datePicker.date = date
    }
    
//    MARK: - Event
    
    var confirm_pick_closure:((Date)->())?
    
    @objc func confirm_picker_action(){
        
        confirm_pick_closure?(self.datePicker.date)
        
        cancel_picker_action()
    }
    
    
    @objc func cancel_picker_action(){
        
        self.isHidden = true
        
    }
    
}
